package com.college.heyuhua.controller;

import com.college.heyuhua.model.FoundInfo;
import com.college.heyuhua.model.LostInfo;
import com.college.heyuhua.model.Thanks;
import com.college.heyuhua.service.IFoundService;
import com.college.heyuhua.service.ILostService;
import com.college.heyuhua.service.IThanksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵东坡 on 2019/2/5.
 * Time:16:57
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
public class LostController {

    @Autowired
    private ILostService iLostService;


    @Autowired
    private IFoundService iFoundService;


    @Autowired
    private IThanksService iThanksService;

    @RequestMapping("/lostList")
    public String lostList(ModelMap map) {

        List<LostInfo> lostInfoList = null;
        List<FoundInfo> foundInfoList = null;
        lostInfoList = iLostService.lostList();
        foundInfoList = iFoundService.foundList();
        Thanks thanks = iThanksService.getOneThanks();
        map.put("lostInfoList",lostInfoList);
        map.put("foundInfoList",foundInfoList);
        map.put("thanks",thanks);
        return "home";
    }

    @RequestMapping("/contract/lid/{lid}")
    public String contract(@PathVariable("lid") String lid, HttpServletResponse response) {

        if (StringUtils.isEmpty(lid)){
            return "404";
        }
        LostInfo info = null;
        info = iLostService.getLostById(Integer.parseInt(lid));
        List<LostInfo> lostInfos = new ArrayList<>();
        lostInfos.add(info);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(info != null) {
            JSONArray listArray=JSONArray.fromObject(lostInfos);
            writer.print(listArray.toString());
            writer.close();
            writer.flush();
        }

        return "404";
    }


    @RequestMapping("/addFound")
    public void addFound(@RequestParam("foundthings") String foundthings,
                        @RequestParam("foundaddress") String foundaddress,
                        @RequestParam("founddate") String founddate,
                        @RequestParam("contract") String contract,
                        @RequestParam("telephone") String telephone,
                        HttpServletResponse response) {

        FoundInfo  foundInfo = new FoundInfo();
        foundInfo.setUid(1l);
        foundInfo.setTelephone(telephone);
        foundInfo.setContract(contract);
        foundInfo.setFoundaddress(foundaddress);
        foundInfo.setFounddate(founddate);
        foundInfo.setFoundthings(foundthings);


        boolean flag = false;
        try{
            flag=iFoundService.insertFoundInfo(foundInfo);
        }catch (Exception e)
        {
            System.out.println(e.getMessage());
        }

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(flag?"提交成功":"提交失败");
        writer.close();
        writer.flush();
    }




    @RequestMapping("/addLost")
    public void addLost(@RequestParam("lostthings") String lostthings,
                          @RequestParam("lostaddress") String lostaddress,
                          @RequestParam("lostdate") String lostdate,
                          @RequestParam("contract") String contract,
                          @RequestParam("telephone") String telephone,
                          HttpServletResponse response) {

        LostInfo lostInfo = new LostInfo();
        lostInfo.setContract(contract);
        lostInfo.setLostaddress(lostaddress);
        lostInfo.setLostdate(lostdate);
        lostInfo.setLostthings(lostthings);
        lostInfo.setTelephone(telephone);
        lostInfo.setUid(1l);

        boolean flag = false;
        try{
            flag=iLostService.insertLostInfo(lostInfo);
        }catch (Exception e)
        {
            System.out.println(e.getMessage());
        }


        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(flag?"提交成功":"提交失败");
        writer.close();
        writer.flush();
    }







}
